package com.itheima.controller;

import com.itheima.interceptor.UserHolder;
import com.itheima.service.SettingsService;
import com.itheima.service.UserService;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    /**
     * 查询通用设置
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     *
     * @param map
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //获取参数
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置-保存
     *
     * @param map
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {

        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单-翻页列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize) {
        //调用service查询
        PageResult pr = settingsService.blacklist(page, pagesize);
        //构造返回
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消黑名单
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlacklist(@PathVariable("uid") Long blackUserId) {
        settingsService.deleteBlacklist(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号-发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userService.sendMsg(UserHolder.getMobile());
        return ResponseEntity.ok(null); //正常返回状态码200
    }

    /**
     * 修改手机号-校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        log.info("需要校验的验证码{}", map.get("verificationCode"));
        boolean verification = settingsService.checkVerificationCode((String) map.get("verificationCode"));
        Map<String, Boolean> result = new HashMap<>();
        result.put("verification", verification);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改手机号-保存新的手机号
     */

    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map) {
        log.info("新的手机号{}", map.get("phone"));
        String phone = (String) map.get("phone");
        settingsService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }
}
